{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image, ImageSequence\n",
    "\n",
    "# 目标尺寸\n",
    "target_width = 240\n",
    "target_height = 240\n",
    "\n",
    "# 打开GIF图片文件\n",
    "gif = Image.open('wukong.gif')\n",
    "\n",
    "# 创建一个用于存储每一帧的图片列表\n",
    "frames = []\n",
    "\n",
    "# 逐帧提取并处理图片\n",
    "for frame in ImageSequence.Iterator(gif):\n",
    "    # 缩放图片\n",
    "    scale_ratio = target_height / frame.height\n",
    "    resized_frame = frame.resize((int(frame.width * scale_ratio), target_height))\n",
    "    \n",
    "    # 裁剪图片\n",
    "    width, height = resized_frame.size\n",
    "    left = (width - target_width) // 2\n",
    "    if left <0 :\n",
    "        left = 0\n",
    "        right = width\n",
    "    else:\n",
    "        right = left + target_width\n",
    "    cropped_frame = resized_frame.crop((left, 0, right, target_height))\n",
    "    \n",
    "    # 添加处理后的帧到列表中\n",
    "    frames.append(cropped_frame)\n",
    "\n",
    "# 创建一个新的GIF文件\n",
    "new_gif = frames[0]\n",
    "\n",
    "# 添加所有处理后的帧到新的GIF文件中\n",
    "new_gif.save('wukongn.gif', save_all=True, append_images=frames[1:], optimize=False, loop=0)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://lvgl.io/tools/imageconverter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
